//
//  ZhuyeViewController.m
//  mid-view
//
//  Created by sus183 on 2020/12/3.
//  Copyright © 2020 itlab. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "ZhuyeViewController.h"
#import "ZhuyeView.h"
#import "PostCell.h"
#import  "CommentViewController.h"
#import "SingleCard.h"
#import "EditingViewController.h"
@interface ZhuyeViewController()<UITableViewDataSource,UITableViewDelegate>{}
@end
@implementation ZhuyeViewController
-(id)init{
    self = [super init];
    if(self){
        leftMargin = 20.0;
        userAreaYCor = 182;
        tableViewYCor = 182;
        postArray = [[NSMutableArray alloc]init];
        self.navigationItem.title = @"主页";
        // 初始数据：
        // 亚利桑那州·菲尼克斯和宾夕法尼亚州·费城
        SingleCard *card1 = [[SingleCard alloc] init]; SingleCard *card2 = [[SingleCard alloc] init];
        [card1 setName:@"Kiko"]; [card1 setTime:@"2020-09-09"]; [card1 setTags:@"#Philadelphia"]; [card1 setContent:@"Hello there"];
        [card2 setName:@"Kiko"]; [card2 setTime:@"2020-10-10"]; [card2 setTags:@"#Guangzhou"]; [card2 setContent:@"In SYSU"];
        [postArray addObject:card1];
        [postArray addObject:card2];
        // 按时间排序
        [postArray sortUsingComparator:^NSComparisonResult(SingleCard *c1, SingleCard *c2) {
            NSComparisonResult result = [[c1 getTime] compare:[c2 getTime]];
            return result;
        }];
    }
    return self;
}
/*
-(void)loadView{
    UINavigationController *navController = [[UINavigationController alloc]init];
    
}
*/
-(void)viewDidLoad{
    self.view = [[ZhuyeView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self setTableView:[[UITableView alloc]initWithFrame:CGRectMake(0,tableViewYCor,self.view.bounds.size.width,self.view.bounds.size.height-tableViewYCor) style:UITableViewStylePlain]];
    [self.tableView setDataSource:self];
    [self.tableView setDelegate:self];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0,0,self.tableView.bounds.size.width,100)];
    
    UIImageView *face = [[UIImageView alloc]initWithFrame:CGRectMake(leftMargin, 0, 100,100)];
    //face.layer.borderWidth = 1.0;
    //face.layer.borderColor = [[UIColor blackColor]CGColor];
    UIImage *image = [UIImage imageNamed:@"profilePic.jpg"];
    [face setImage:image];
    [header addSubview:face];
    
    UILabel *userName = [[UILabel alloc]initWithFrame:CGRectMake(leftMargin+100+10,0, 200,40)];
    userName.text = @"UserName";
    [header addSubview:userName];
    self.tableView.tableHeaderView = header;
    [self.view addSubview:self.tableView];
    [self addPlusButton];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return postArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PostCell *cell = [[PostCell alloc]init];
    [cell updateCellWithMode:postArray[indexPath.row]];
    cell.likeButton.tag = indexPath.row;
    cell.commentButton.tag = indexPath.row;
    cell.starButton.tag = indexPath.row;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CommentViewController *commentViewController = [[CommentViewController alloc] init];
    [commentViewController setContent:postArray[indexPath.row]];
    //selectedRow = postArray[indexPath.row];
    // Return Form Detail
    CATransition *tran = [CATransition animation];
    tran.delegate = self;
    tran.type = @"push";
    tran.subtype=kCATransitionFromRight;
    [self.navigationController.view.layer addAnimation:tran forKey:nil];
    [self.navigationController pushViewController:commentViewController animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 160;
}

//隐藏下面的线条
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger) section{
    return [[UIView alloc]init];
}

-(void)addPlusButton{
    UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(330,700,70,70)];
    [button setBackgroundImage:[UIImage imageNamed:@"plusButton.png"]forState:UIControlStateNormal];
    button.layer.masksToBounds =YES;
    button.layer.cornerRadius = 35;
    [button addTarget:self action: @selector(moveToEditView:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:button];
}

-(void)moveToEditView:(id)obj{
    EditingViewController *view = [[EditingViewController alloc]init];
    [self.navigationController pushViewController:view animated:YES];
    //[self presentViewController:view animated:YES completion:nil];
}

@end
